<?php
/**
 * 
 * @author Paolo Bertuzzo
 *
 */
namespace User\Data;

use User\Entity\CustomerAddress;

class CustomerAddressDao extends AbstractDao
{
    
    /**
     * 
     * @param integer $customerId
     */
    public function get($customerId) 
    {
        $query = $this->entityManager->createQuery('select ca from User\Entity\CustomerAddress ca where ca.customerId = ?1');
        
        $query->setParameter(1, $customerId);
        
        return $query->getResult();
    }

    /**
     * 
     * @param integer $customerId
     * @param string $type
     */
    public function getByType($customerId, $type)
    {
    	$query = $this->entityManager->createQuery('select ca from User\Entity\CustomerAddress ca where ca.customerId = ?1 and ca.type = ?2');
    
    	$query->setParameter(1, $customerId);
    	$query->setParameter(2, $type);
    	 
        $result = $query->getResult();
        
        return count($result) == 0 ? null : $result[0];
    }

    /**
     * 
     * @param integer $customerId
     * @param string $type
     * @param string $street
     * @param string $number
     * @param string $city
     * @param string $state
     * @param string $country
     * @param string $zipCode
     * @param integer $stateId
     * @param integer $countryId
     */
    public function insert($customerId, $type, $street, $number, $city, $zipCode, $state, $country, $stateId, $countryId)
    {
    	$customerAddress = new CustomerAddress();
        
        $customerAddress->setCustomerId($customerId);
    	$customerAddress->setType($type);
        $customerAddress->setCity($city);
        $customerAddress->setCountry($country);
        $customerAddress->setNumber($number);
        $customerAddress->setState($state);
        $customerAddress->setZipCode($zipCode);
        $customerAddress->setStateId($stateId);
        $customerAddress->setCountryId($countryId);
        $customerAddress->setStreet($street);
        
    	$this->entityManager->persist($customerAddress);
    	$this->entityManager->flush();
    	
    	return $customerAddress;
    }
    
    /**
     *
     * @param integer $customerAddressId
     * @param string $street
     * @param string $number
     * @param string $city
     * @param string $state
     * @param string $country
     * @param string $zipCode
     * @param integer $stateId
     * @param integer $countryId
     */
    public function update($customerAddressId, $street, $number, $city, $zipCode, $state, $country, $stateId, $countryId)
    {
        $query = $this->entityManager->createQuery('select ca from User\Entity\CustomerAddress ca where ca.customerAddressId = ?1');
        
        $query->setParameter(1, $customerAddressId);

        if (count($result) == 0)
        	$this->logger->info(sprintf('Customer address not found for id %d', $customerAddressId));
        
        $customerAddress = $query->getResult()[0];
        
        $customerAddress->setCity($city);
        $customerAddress->setCountry($country);
        $customerAddress->setNumber($number);
        $customerAddress->setState($state);
        $customerAddress->setStreet($street);
        $customerAddress->setZipCode($zipCode);
        $customerAddress->setStateId($stateId);
        $customerAddress->setCountryId($countryId);
        
    	$this->entityManager->merge($customerAddress);
    	$this->entityManager->flush();
    	
    	return $customerAddress;
    }
    
}

?>